<template>
    <div class="layout">
        <el-container v-if="dataInfo.showMenu" class="container">
            <el-aside class="aside">
                <div class="head">
                    <div>
                        <img src="@/assets/logo.jpg" alt="logo">
                        <span>商家端</span>
                    </div>
                </div>
                <div class="line"/>
                <el-menu
                        v-if="hasShop"
                        background-color="#222832"
                        text-color="#fff"
                        :router="true"
                        :default-openeds="dataInfo.defaultOpen"
                        :default-active='dataInfo.currentPath'
                >

                    <el-sub-menu index="1">
                        <template #title>
                            <span>账户管理</span>
                        </template>
                        <el-menu-item-group>
                            <el-menu-item index="/">
                                <el-icon>
                                    <Odometer/>
                                </el-icon>
                                首页
                            </el-menu-item>
                            <el-menu-item index="/modify">
                                <el-icon>
                                    <Plus/>
                                </el-icon>
                                资料修改
                            </el-menu-item>
                            <el-menu-item index="/account">
                                <el-icon>
                                    <Lock/>
                                </el-icon>
                                修改密码
                            </el-menu-item>
                        </el-menu-item-group>
                    </el-sub-menu>
                    <el-sub-menu index="2">
                        <template #title>
                            <span>商品管理</span>
                        </template>
                        <el-menu-item-group>
                            <el-menu-item index="/good">
                                <el-icon>
                                    <Goods/>
                                </el-icon>
                                商品清单
                            </el-menu-item>
                            <el-menu-item index="/addgood">
                                <el-icon>
                                    <Menu/>
                                </el-icon>
                                增加商品
                            </el-menu-item>
                            <el-menu-item index="/examine">
                                <el-icon>
                                    <Menu/>
                                </el-icon>
                                审核列表
                            </el-menu-item>
                        </el-menu-item-group>
                    </el-sub-menu>
                   <!-- <el-sub-menu index="4">
                        <template #title>
                            <span>系统管理</span>
                        </template>
                        <el-menu-item-group>

                        </el-menu-item-group>
                    </el-sub-menu>-->
                    <el-sub-menu index="3">
                        <template #title>
                            <span>订单管理</span>
                        </template>
                        <el-menu-item-group>
<!--                            <el-menu-item index="/neworders">
                                <el-icon>
                                    <Goods/>
                                </el-icon>
                                最新订单
                            </el-menu-item>-->
                            <el-menu-item index="/test">
                                <el-icon>
                                    <Goods/>
                                </el-icon>
                                最新订单
                            </el-menu-item>
                            <el-menu-item index="/orders">
                                <el-icon>
                                    <Goods/>
                                </el-icon>
                                全部订单
                            </el-menu-item>
                        </el-menu-item-group>
                    </el-sub-menu>
                    <el-sub-menu index="4">
                        <template #title>
                            <span>数据统计</span>
                        </template>
                        <el-menu-item-group>
                            <el-menu-item index="/finance">
                                <el-icon>
                                    <Goods/>
                                </el-icon>
                                财务明细
                            </el-menu-item>
                        </el-menu-item-group>
                        <el-menu-item-group>
                            <el-menu-item index="/statistics">
                                <el-icon>
                                    <Goods/>
                                </el-icon>
                                最近营业情况
                            </el-menu-item>
                        </el-menu-item-group>
                    </el-sub-menu>
                </el-menu>
               <el-menu
                       v-else
                       background-color="#222832"
                       text-color="#fff"
                       :router="true"
                       :default-openeds="dataInfo.defaultOpen"
                       :default-active='dataInfo.currentPath'>
                   <el-sub-menu index="1">
                       <template #title>
                           <span>菜单</span>
                       </template>
                       <el-menu-item-group>
                           <el-menu-item index="/setup">
                               <el-icon>
                                   <Odometer/>
                               </el-icon>
                               开通店铺
                           </el-menu-item>
                           <el-menu-item index="/account">
                               <el-icon>
                                   <Plus/>
                               </el-icon>
                               修改密码
                           </el-menu-item>
                       </el-menu-item-group>
                   </el-sub-menu>
               </el-menu>
            </el-aside>
            <el-container class="content">

                <Header/>
                <div class="main">
                    <router-view/>
                </div>
                <Footer/>

            </el-container>
        </el-container>
        <el-container v-else class="container">
            <router-view/>
        </el-container>
    </div>
</template>

<script setup>
    import {reactive} from 'vue'
    import {useRouter} from 'vue-router'
    import Header from '@/components/Header.vue'
    import Footer from '@/components/Footer.vue'
    import {useStore} from 'vuex'
    import {ref} from 'vue'
    const store = useStore()
    const noMenu = ['/login','/register']
    const router = useRouter()
    const dataInfo = reactive({
        showMenu: true,
        defaultOpen: ['1', '2', '3', '4'],
        currentPath: '/',
    })
    let hasShop=ref(true);
    router.beforeEach((to, from, next) => {
        if (to.path === '/login'||to.path=='/register') {
            // 如果路径是 /login 则正常执行
            next()
        } else {
            // 如果不是 /login，判断是否有 token
            if (!store.getters.getToken || store.getters.getToken === '') {
                // 如果没有，则跳至登录页面
                next({path: '/login'})
            } else {
                // 否则继续执行
                next()
            }
        }
        dataInfo.showMenu = !noMenu.includes(to.path)
        dataInfo.currentPath = to.path
        hasShop.value=store.getters.getShop
        console.log(store.getters.getShop)
    })
</script>

<style scoped>
    .layout {
        min-height: 100vh;
        background-color: #ffffff;
    }

    .container {
        height: 100vh;
    }

    .aside {
        width: 200px !important;
        background-color: #222832;
    }

    .head {
        display: flex;
        align-items: center;
        justify-content: center;
        height: 50px;
    }

    .head > div {
        display: flex;
        align-items: center;
    }

    .head img {
        width: 50px;
        height: 50px;
        margin-right: 10px;
    }

    .head span {
        font-size: 20px;
        color: #ffffff;
    }

    .line {
        border-top: 1px solid hsla(0, 0%, 100%, .05);
        border-bottom: 1px solid rgba(0, 0, 0, .2);
    }

    .content {
        display: flex;
        flex-direction: column;
        max-height: 100vh;
        overflow: hidden;
    }

    .main {
        height: calc(100vh - 100px);
        overflow: auto;
        padding: 10px;
    }
</style>

<style>
    body {
        padding: 0;
        margin: 0;
        box-sizing: border-box;
    }

    .el-menu {
        border-right: none !important;
    }

    .el-submenu {
        border-top: 1px solid hsla(0, 0%, 100%, .05);
        border-bottom: 1px solid rgba(0, 0, 0, .2);
    }

    .el-submenu:first-child {
        border-top: none;
    }

    .el-submenu [class^="el-icon-"] {
        vertical-align: -1px !important;
    }

    a {
        color: #409eff;
        text-decoration: none;
    }

    .el-pagination {
        text-align: center;
        margin-top: 20px;
    }

    .el-popper__arrow {
        display: none;
    }
</style>
